﻿package as3app.filterPanels
{
	import com.adobe.examples.flash.ComboSlider;
	import com.example.programmingas3.filterWorkbench.BlurFactory;
	import com.example.programmingas3.filterWorkbench.IFilterFactory;
	import com.example.programmingas3.filterWorkbench.IFilterPanel;
	
	import fl.controls.ComboBox;
	import fl.data.DataProvider;
	
	import flash.events.Event;
	import flash.filters.BitmapFilterQuality;
	import flash.display.Sprite;
	import myLables;
	
	public class BlurPanel extends Sprite implements IFilterPanel
	{
		// ------- Private vars -------
		private var _filterFactory:BlurFactory;
		
		// ------- Child Controls -------
		// Positioned and created within FilterWorkbench.fla
		public var blurXValue:ComboSlider=new ComboSlider();
		public var blurYValue:ComboSlider=new ComboSlider();
		public var qualityValue:ComboBox=new ComboBox();
		
		
		// ------- Constructor -------
		public function BlurPanel()
		{
			initshow();
			addEventListener(Event.ADDED, setupChildren);
		}
		
		private function initshow():void
		{
			//初始化显示组件
			addChild(myLables.getALable("X轴模糊:", 0, 6, 50, 22, "right"));
			addChild(myLables.getALable("Y轴模糊:", 0, 34, 50, 22, "right"));
			addChild(myLables.getALable("品质:", 0, 90, 50, 22, "right"));
			
			blurXValue.x = 60;
			blurXValue.y = 6;
			blurXValue.width = 47;
			blurXValue.height = 22;
			blurXValue.maximum = 255;
			blurXValue.value = 4;
			addChild(blurXValue);
			
			blurYValue.x = 60;
			blurYValue.y = 34;
			blurYValue.width = 47;
			blurYValue.height = 22;
			blurYValue.maximum = 255;
			blurYValue.value = 4;
			addChild(blurYValue);
			
			qualityValue.x = 60;
			qualityValue.y = 90;
			qualityValue.width = 96;
			qualityValue.height = 22;
			qualityValue.rowCount = 5;
			addChild(qualityValue);
		}
		
		// ------- Public Properties -------
		public function get filterFactory():IFilterFactory
		{
			return _filterFactory;
		}
		
		
		// ------- Public methods -------
		public function resetForm():void
		{
			blurXValue.value = 4;
			blurYValue.value = 4;
			qualityValue.selectedIndex = 0;
			
			if (_filterFactory != null)
			{
				_filterFactory.modifyFilter();
			}
		}
		
		
		// ------- Event Handling -------
		private function setupChildren(event:Event):void
		{
			removeEventListener(Event.ADDED, setupChildren);
			
			// create the filter factory
			_filterFactory = new BlurFactory();
			
			// populate the quality combo box
			var qualityList:DataProvider = new DataProvider();
			qualityList.addItem({label:"低", data:BitmapFilterQuality.LOW});
			qualityList.addItem({label:"中", data:BitmapFilterQuality.MEDIUM});
			qualityList.addItem({label:"高", data:BitmapFilterQuality.HIGH});
			qualityValue.dataProvider = qualityList;
			
			// add event listeners for child controls
			blurXValue.addEventListener(Event.CHANGE, changeFilterValue);
			blurYValue.addEventListener(Event.CHANGE, changeFilterValue);
			qualityValue.addEventListener(Event.CHANGE, changeFilterValue);
		}
		
		
		private function changeFilterValue(event:Event):void
		{
			// update the filter
			updateFilter();
		}
		
		
		// ------- Private methods -------
		private function updateFilter():void
		{
			var blurX:Number = blurXValue.value;
			var blurY:Number = blurYValue.value;
			var quality:int = qualityValue.selectedItem.data;
			
			_filterFactory.modifyFilter(blurX, blurY, quality);
		}
	}
}